import { Body, Controller, Get, Patch, Post, Query } from '@nestjs/common';
import { UserService } from './user.service';
import {
  LoginUserDto,
  RegisterUserDto,
  UpdateUserDto,
  UpdateUserPasswordDto,
} from './user.dto';
import { RequireLogin, UserInfo } from 'src/custom.decorator';

@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) {}

  @Post('register-captcha')
  async sendVerifyCode(@Body() body) {
    return await this.userService.generateVerifyCode(body.email);
  }

  @Post('register')
  async register(@Body() registerUser: RegisterUserDto) {
    return await this.userService.create(registerUser);
  }

  @Post('login')
  async login(@Body() loginUser: LoginUserDto, @UserInfo('username') username) {
    console.log(username);
    return await this.userService.login(loginUser);
  }

  @Get('info')
  @RequireLogin()
  async info(@UserInfo('userId') userId: number) {
    console.log(userId);
    return this.userService.findUserDetailById(userId);
  }

  @Post('update_info/captcha')
  @RequireLogin()
  async updateInfoCaptcha(@Body('email') email: string) {
    return await this.userService.generateUpdateInfoCaptcha(email);
  }

  @Patch('info/update')
  @RequireLogin()
  async update(
    @UserInfo('userId') userId: number,
    @Body() updateUserDto: UpdateUserDto,
  ) {
    return await this.userService.update(userId, updateUserDto);
  }

  @Post('update_password/captcha')
  async updatePasswordCaptcha(@Body('email') email: string) {
    return this.userService.generateUpdatePasswordCaptcha(email);
  }

  @Post('update_password')
  async updatePassword(@Body() passwordDto: UpdateUserPasswordDto) {
    return this.userService.updatePassword(passwordDto);
  }

  @Get('friendship')
  @RequireLogin()
  async friendship(@UserInfo('userId') userId: number) {
    return this.userService.getFriendship(userId);
  }
}
